package com.examples.listeners;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Practice {

	private Text firstNameText;
	private Text lastNameText;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setText("Outer Shell");
		DispoListener disposeListener = new DispoListener();

		Practice practice = new Practice();
		practice.createDialogArea(shell).open();

		/* We can create an untyped listener */

		/*
		 * Listener listener = new Listener() { public void handleEvent(Event
		 * event) { switch (event.type) { case SWT.Dispose: break; case
		 * SWT.MouseDown: break; case SWT.MouseUp: break; case SWT.MouseMove:
		 * break; } System.out.println("Something happened."); } };
		 * shell.addListener(SWT.Dispose, listener);
		 * shell.addListener(SWT.MouseDown, listener);
		 * shell.addListener(SWT.MouseUp, listener);
		 * shell.addListener(SWT.MouseMove, listener);
		 */
		Shell shell1 = new Shell(shell.getDisplay());
		shell1.setText("Inner Shell");
		shell1.open();
		shell.addDisposeListener(disposeListener);
		while (!shell.isDisposed()) {
			/*
			 * Checking if OS has any other event in queue, If not it goes
			 * inside if
			 */
			if (!display.readAndDispatch()) {
				/* Asking the display to remain visible */
				display.sleep();
			}
		}
		display.dispose();
	}

	/*
	 * Passed an empty shell and returned the same shell with 2 text fields and
	 * buttons
	 */
	public Shell createDialogArea(Shell parent) {

		GridLayout layout = new GridLayout();
		layout.numColumns = 2;

		parent.setLayout(layout);

		FocusListener listener = new TextFieldFocusListener();

		GridData gridData = new GridData();

		Label label1 = new Label(parent, SWT.NONE);
		label1.setText("First Name");
		firstNameText = new Text(parent, SWT.BORDER);
		firstNameText.setLayoutData(gridData);
		firstNameText.addFocusListener(listener);

		gridData = new GridData();

		Label label2 = new Label(parent, SWT.NONE);
		label2.setText("Last Name");
		lastNameText = new Text(parent, SWT.BORDER);
		lastNameText.setLayoutData(gridData);

		MouseListener mouseListener = new MouseHoverListener();

		MouseMoveListener mouseMoveListener = new Mmove();
		label2.addMouseMoveListener(mouseMoveListener);

		Button button = new Button(parent, SWT.NONE);
		button.setText("B1");
		button.addMouseListener(mouseListener);
		// button.notifyListeners(SWT.MouseUp, new Event());
		return parent;
	}

}
